package com.qyhl.module_home.utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

public class IndexBarView extends View {
    private Context context;
    private String[] datas;
    private Paint mPaint;
    private int width;
    private int height;
    private float textOffset;
    private int selectPosition = 0;
    private IndexOnClickListener indexOnClickListener;

    public IndexBarView(Context context) {
        this(context, null);
    }

    public IndexBarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndexBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void init() {
        datas = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"};
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthmode = MeasureSpec.getMode(widthMeasureSpec);
        int widthsize = MeasureSpec.getSize(widthMeasureSpec);
        if (widthmode == MeasureSpec.EXACTLY) {
            width = widthsize;
        } else if (widthmode == MeasureSpec.AT_MOST) {
            mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, context.getResources().getDisplayMetrics()));
            Rect rect = new Rect();
            mPaint.getTextBounds(datas[0], 0, 1, rect);
            width = Math.min(rect.width() + getPaddingStart() + getPaddingEnd(), widthsize);
        }

        int heightmode = MeasureSpec.getMode(heightMeasureSpec);
        int heightsize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightmode == MeasureSpec.EXACTLY) {
            height = heightsize;
        } else if (heightmode == MeasureSpec.AT_MOST) {
            mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, context.getResources().getDisplayMetrics()));
            Rect rect = new Rect();
            mPaint.getTextBounds(datas[0], 0, 1, rect);
            height = Math.min(rect.height() * datas.length + getPaddingTop() + getPaddingBottom(), heightsize);
        }
        setTextSize();
        setMeasuredDimension(width, height);
    }

    private void setTextSize() {
        Rect rect = new Rect();
        float size = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, context.getResources().getDisplayMetrics());
        mPaint.setTextSize(size--);
        mPaint.getTextBounds(datas[0], 0, 1, rect);
        int itemHeight = (height - getPaddingTop() - getPaddingBottom()) / datas.length;
        while (rect.height() > itemHeight ||
                (rect.width() + getPaddingStart() + getPaddingEnd()) > width) {
            mPaint.setTextSize(size--);
            mPaint.getTextBounds(datas[0], 0, 1, rect);
        }

        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
        textOffset = fontMetrics.top / 2 + fontMetrics.bottom / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setColor(Color.parseColor("#1D1D1D"));
        int itemHeight = height / datas.length;
        for (int i = 0; i < datas.length; i++) {
            if (selectPosition == i) {
                mPaint.setColor(Color.parseColor("#E53935"));
                canvas.drawCircle(width / 2, getPaddingTop() + itemHeight * i + itemHeight / 2,
                        width < itemHeight ? width / 2 : itemHeight / 2, mPaint);
                mPaint.setColor(Color.parseColor("#FFFFFF"));
                canvas.drawText(datas[i], width / 2,
                        getPaddingTop() + itemHeight * i + itemHeight / 2 - textOffset, mPaint);
                mPaint.setColor(Color.parseColor("#1D1D1D"));
            } else {
                canvas.drawText(datas[i], width / 2,
                        getPaddingTop() + itemHeight * i + itemHeight / 2 - textOffset, mPaint);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            int y = (int) event.getY();
            int itemHeight = (height - getPaddingTop() - getPaddingBottom()) / datas.length;
            if (y < itemHeight + getPaddingTop()) {
                selectPosition = 0;
            } else {
                y = y - itemHeight - getPaddingTop();
                selectPosition = Math.min(y / itemHeight + 1, datas.length - 1);
            }
        }
        indexOnClickListener.indexOnClick(selectPosition, datas[selectPosition]);
        invalidate();
        return true;
    }

    public void moveToPositionFormTag(String tag) {
        for (int i = 0; i < datas.length; i++) {
            if (tag.equals(datas[i])) {
                selectPosition = i;
                invalidate();
                break;
            }
        }
    }

    public void setIndexOnClickListener(IndexOnClickListener indexOnClickListener) {
        this.indexOnClickListener = indexOnClickListener;
    }

    public interface IndexOnClickListener {
        void indexOnClick(int selectPosition, String selectTag);
    }
}
